package com.letv.business.flow.PlayFlowState;

import android.app.Application;
import android.text.TextUtils;

import com.ledim.app.LeDimApplication;
import com.letv.android.young.client.R;
import com.letv.business.flow.PlayFlowEvents.PlayAuthEvent;
import com.letv.business.flow.PlayFlowModule.NetworkManager;
import com.letv.business.flow.album.AlbumPlayFlowObservable;
import com.letv.business.flow.album.model.AlbumPlayInfo;
import com.letv.core.bean.DataHull;
import com.letv.core.bean.PlayRecord;
import com.letv.core.bean.VideoBean;
import com.letv.core.bean.VideoFileBean;
import com.letv.core.bean.VideoPlayerBean;
import com.letv.core.network.volley.VolleyResponse;
import com.letv.core.utils.BaseTypeUtils;
import com.letv.core.utils.LetvUtils;
import com.letv.core.utils.LogInfo;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

/**
 * Created by jipengfei on 2016/10/31.
 */
public class PlayFetchMetaState extends BasePlayState{

    @Inject
    NetworkManager networkManager;

    @Override
    protected void injectDependencies() {
        LeDimApplication.getComponentInstance().inject(this);
    }

    @Override
    public void onStateApplied() {
        super.onStateApplied();

        LogInfo.log("sguotao", "进入FetchMeta State");

        mPlayFlow = getProvider().providPlayFlow();

        networkManager.setRequestVFListener(new NetworkManager.requestVFListener(){
            @Override
            public  void  requestVFSuccess(VideoPlayerBean result, DataHull hull){
               mPlayFlow.getAlbumPlayingListener().setPlayingVideoBean(result.getVideo());
                initVideo(result.getVideo());
                initVideoFile(result.getVideoFile());

                mPlayFlow.setPayInfo(result.getPayInfo());
                mPlayFlow.setmVideoFile(result.getVideoFile());
                mPlayFlow.getObserver().notifyObservers(AlbumPlayFlowObservable.REFRESH_DATA_AFTER_REQUEST_VIDEO_URL);

                fireEvent(PlayFlowEvents.Play_Auth);

            }

            @Override
            public void requestVFFailure(VolleyResponse.NetworkResponseState state){

                mPlayFlow.setPlayCallBackState(2);
                fireEvent(PlayFlowEvents.Play_Error);
            }
        });

    }

    @Override
    public void onStateLeft() {
        super.onStateLeft();

        networkManager.setRequestVFListener(null);
//        getActionInterface().hideLoginLoading();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPlayAuthEvent(PlayAuthEvent playAuthEvent) {

//        playAuthEvent.checkCanPlay(result.getPayInfo(), result.getVideoFile(), hull);

        fireEvent(PlayFlowEvents.Play_Auth);
    }

    @Subscribe
    public void onPlayErrorEvent(PlayErrorState playErrorState) {
        fireEvent(PlayFlowEvents.Play_Error);
    }

    /**
     * 初始化video信息
     *
     * @param video
     */
    private void initVideo(VideoBean video) {
        mPlayFlow.setVideoBean(video);

        VideoBean mCurrentPlayingVideo =  mPlayFlow.getPlayingVideo();
        if (mCurrentPlayingVideo == null) {
            return;
        }

        AlbumPlayInfo mPlayInfo = mPlayFlow.getPlayInfo();
        mPlayInfo.totalTime = mCurrentPlayingVideo.getDuration() * 1000;
        mPlayFlow.setPlayInfo(mPlayInfo);

        mPlayFlow.setAid(mCurrentPlayingVideo.getPid());
        mPlayFlow.setCid(mCurrentPlayingVideo.getCid());
        mPlayFlow.setVid(mCurrentPlayingVideo.getId());

//        PlayRecord mPlayRecord = mPlayFlow.getPlayRecord();
        if (mPlayFlow.getPlayRecord() != null) {
            mPlayFlow.getPlayRecord().setAlbumId((int) mCurrentPlayingVideo.getPid());
        }

        AlbumPlayFlowObservable mObservable = mPlayFlow.getObserver();
        mObservable.notifyObservers(new AlbumPlayFlowObservable.VideoTitleNotify(getTitle(mCurrentPlayingVideo)));

//        if (mIsDownloadFile) {
//            handlerFloatBall();
//        }

//        if (!mIsLocalSuccess) {
            // 本地加载失败
            mPlayFlow.createPlayRecord(true);
            if (mPlayFlow.getPlayRecord() != null) {
                mPlayFlow.getPlayRecord().setTotalDuration(mCurrentPlayingVideo.getDuration());
                mPlayInfo.totalTime = mPlayFlow.getPlayRecord().getTotalDuration() * 1000;
//            }
        }

        mObservable.notifyObservers(AlbumPlayFlowObservable.ON_REQUEST_VIDEO_INFO_SUCCESS);
        mObservable.notifyObservers(AlbumPlayFlowObservable.REFRESH_DATA_AFTER_REQUEST_VIDEO_URL);
//        mPlayFlow.setObservable(mObservable);
    }

    /**
     * 请求回来视频详情，调度地址请求前的逻辑
     */
    protected void initVideoFile(VideoFileBean videoFile) {
//          mVideoFile = videoFile;
        mPlayFlow.setmVideoFile(videoFile);
    }

    /**
     * 设置标题
     *
     * @param video
     */
    private String getTitle(VideoBean video) {
        if (video == null) {
            return "";
        }

        String channelName = LetvUtils.getChannelName(mPlayFlow.getContext(), video.getCid());
        String name = "";
        if (TextUtils.equals(mPlayFlow.getContext().getString(R.string.channel_music), channelName)) {
            String singer = video.getSinger();
            name = video.getNameCn() + "  " + BaseTypeUtils.ensureStringValidate(singer);
        } else {
            name = video.getNameCn();
        }

        return name;
    }

}
